<?php
/**
 * Created by PhpStorm.
 * User: ma
 * Date: 2017/12/30
 * Time: 14:25
 */

namespace app\index\controller;

use think\facade\Request;
use app\common\model\Orders;
use think\facade\Session;


class Order extends  Base

{
    /**
     * 商家订单列表
     */
    public  function orderList(){
        $request=$this->request;
//        if(!$request->param('shop_id')){
//            return false;
//        }
//        $shop_id=$request->param('shop_id');
        $shop_id=Session::get('sp_id','index');
//        var_dump($shop_id);die;
//        $shop_id=1;
        $where = [];
        if (!empty(Request::post('add_time_begin'))) {
            $where[] = ['ord_create_time', '>', strtotime(Request::post('add_time_begin'))];
        }
        if (!empty(Request::post('add_time_end'))) {
            $where[] = ['ord_create_time', '<', strtotime(Request::post('add_time_end'))];
        }
        if (!empty(Request::post('ord_num'))) {
            $where[] = ['ord_num', 'like', '%' . Request::post('ord_num') . '%'];
        }
        if (!empty(Request::post('ord_status'))) {
            $where[] = ['ord_status', '=', Request::post('ord_status')];
        }

        //获取所有订单
        $res=db('orders')
            ->alias('or')
            ->leftJoin('user u','u.id=or.ord_user_id')
            ->leftJoin('shops s','s.sp_id=or.ord_shop_id')
            ->where('ord_shop_id',$shop_id)
            ->where($where)
            ->field('ord_id,ord_num,ord_user_id,nick_name,sp_name,ord_shop_id,ord_status,ord_total_price,ord_real_price,ord_cou_price,ord_cou_id,ord_create_time')
            ->paginate(10,false,[
                'path'=>url('order/orderList',[
                    'where'=>''])
            ]);
        // dump($orderlist);
        $orderCount=$res->count();
        $this->assign('add_time_begin', Request::post('add_time_begin'));
        $this->assign('add_time_end', Request::post('add_time_end'));
        $this->assign('ord_num', Request::post('ord_num'));
        $this->assign('ord_status', Request::post('ord_status'));
        $this->assign('ordercount', $orderCount);
        $this->assign('orderlist', $res);
        return $this->fetch('order');
    }
    /**
     * 导出商户订单
     */
    public  function export_order(){
        $request=$this->request;
        $id=1;
        $strTable ='<table width="500" border="1">';
        $strTable .= '<tr>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:120px;">订单ID</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="100">订单编号</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">用户</td>';
//        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">代理区域</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">商户</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">支付状态</td>';

        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">总金额</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">实付金额</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">优惠金额</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">优惠券id</td>';

        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">下单时间</td>';
//        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">登录时间</td>';
        $strTable .= '</tr>';
        $count =\db('orders')
            ->alias('or')
            ->leftJoin('user u','u.id=or.ord_user_id')
            ->leftJoin('shops s','s.sp_id=or.ord_shop_id')
            ->where('sp_id',1)
            ->field('ord_id,ord_num,ord_user_id,nick_name,sp_name,ord_shop_id,ord_status,ord_total_price,ord_real_price,ord_cou_price,ord_cou_id,ord_create_time')
            ->count();
        $p = ceil($count/5000);
        for($i=0;$i<$p;$i++){
            $start = $i*5000;
            $end = ($i+1)*5000;
            $agentsList = \db('orders')
                ->alias('or')
                ->leftJoin('user u','u.id=or.ord_user_id')
                ->leftJoin('shops s','s.sp_id=or.ord_shop_id')
                ->where('sp_id',1)
                ->order('ord_id')
                ->limit($start.','.$end)
                ->select();


            foreach($agentsList as $k=>$val){
                $strTable .= '<tr>';
                $strTable .= '<td style="text-align:center;font-size:12px;">'.$val['ord_id'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['ord_num'].' </td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['nick_name'].' </td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['sp_name'].' </td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['ord_status'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['ord_total_price'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['ord_real_price'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['ord_cou_price'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['ord_cou_id'].' </td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.date('Y-m-d H:i',$val['ord_create_time']).'</td>';
                $strTable .= '</tr>';
            }
            unset($agentsList);
        }
        $strTable .='</table>';
        downloadExcel($strTable,'orders_'.$i);
        exit();

    }

}